<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class land_static extends CI_Controller {
	var $data = array();

	public function __construct() {
		parent::__construct();
		$this -> output -> cache(0);
		$this -> data['nowmenu'] = "land";
		$this -> data['title'] = "Static Content Management";
		$this -> data['current_module'] = site_url("land_static");
$this -> data['role_id']=$this -> session -> userdata('role_id');
	}

	public function delete_croptype() {
		$this -> db -> update('land_crop_type', array('FLAG_ACTIVE' => $_REQUEST['FLAG_ACTIVE']), "landcrop_type_id=" . $_REQUEST['landcrop_type_id']);
		echo "Delete Complete";
	}

	public function getcroptypebyid($croptypeid) {
		$query = $this -> db -> query("SELECT * FROM land_crop_type where landcrop_type_id=" . $croptypeid);
		$tmp = json_encode($query -> result_array());
		$tmp = str_replace("[", "", $tmp);
		$tmp = str_replace("]", "", $tmp);
		echo $tmp;
	}

	public function add_croptype() {

		if ($_REQUEST['landcrop_type_id'] == "0") {
			$this -> db -> insert('land_crop_type', array('crop_type_name' => $_REQUEST['crop_type_name']));
		} else {
			$this -> db -> update('land_crop_type', array('crop_type_name' => $_REQUEST['crop_type_name']), "landcrop_type_id=" . $_REQUEST['landcrop_type_id']);
		}
		echo "Save Complete";
	}

	public function delete_documenttype() {
		$this -> db -> update('land_document_type', array('FLAG_ACTIVE' => $_REQUEST['FLAG_ACTIVE']), "land_document_type_id=" . $_REQUEST['land_document_type_id']);
		echo "Delete Complete";
	}

	public function getdocumenttypebyid($croptypeid) {
		$query = $this -> db -> query("SELECT * FROM land_document_type where land_document_type_id=" . $croptypeid);
		$tmp = json_encode($query -> result_array());
		$tmp = str_replace("[", "", $tmp);
		$tmp = str_replace("]", "", $tmp);
		echo $tmp;
	}

	public function add_documenttype() {
		
		if ($_REQUEST['land_document_type_id'] == "0") {
			$this -> db -> insert('land_document_type', array('land_document_type_name' => $_REQUEST['land_document_type_name']));
		} else {
			$this -> db -> update('land_document_type', array('land_document_type_name' => $_REQUEST['land_document_type_name']), "land_document_type_id=" . $_REQUEST['land_document_type_id']);
		}
		echo "Save Complete";
	}

	public function getcroptype() {
		$query = $this -> db -> query('SELECT * FROM land_crop_type');
		$htmlResult = "<table id=\"croptable\" class=\"listing\" cellpadding=\"0\" cellspacing=\"0\">
			<tr>
				<th>ประเภทการเพาะปลูก</th>
				<th style=\"width: 80px;\">แก้ไข</th>
				<th style=\"width: 80px;\">ลบ</th>
			</tr>";

		foreach ($query->result() as $row) {
			$htmlResult .= "<tr>";
			$htmlResult .= "<td>" . $row -> crop_type_name . "</td>";
			$htmlResult .= "<td align=\"center\"><a href=\"javascript:edit('" . $row -> landcrop_type_id . "')\">" . "<img src='" . base_url() . "images/icon_edit.png'/></a></td>";
			$htmlResult .= "<td align=\"center\"><a href=\"javascript:delete('" . $row -> landcrop_type_id . "','" . $row -> FLAG_ACTIVE . "')\">" . "<img src='" . base_url() . "images/icon_delete.png'/></a></td>";
			$htmlResult .= "</tr>";

		}
		if ($query -> num_rows() == 0) {
			$htmlResult .= "<tr>";
			$htmlResult .= "<td colspan='3'>No Data</td>";
			$htmlResult .= "</tr>";
		}
		$htmlResult .= "</table>";
		echo $htmlResult;
	}

	public function getdocumenttype() {
		$query = $this -> db -> query('SELECT * FROM  `land_document_type`');
		$htmlResult = "<table id=\"croptable\" class=\"listing\" cellpadding=\"0\" cellspacing=\"0\">
			<tr>
				<th>ประเภทเอกสารสิทธิ</th>
				<th style=\"width: 80px;\">แก้ไข</th>
				<th style=\"width: 80px;\">ลบ</th>
			</tr>";

		foreach ($query->result() as $row) {
			$htmlResult .= "<tr>";
			$htmlResult .= "<td>" . $row -> land_document_type_name . "</td>";
			$htmlResult .= "<td align=\"center\"><a href=\"javascript:edit('" . $row -> land_document_type_id . "')\">" . "<img src='" . base_url() . "images/icon_edit.png'/></a></td>";
			$htmlResult .= "<td align=\"center\"><a href=\"javascript:delete('" . $row -> land_document_type_id . "','" . $row -> FLAG_ACTIVE . "')\">" . "<img src='" . base_url() . "images/icon_delete.png'/></a></td>";
			$htmlResult .= "</tr>";

		}
		if ($query -> num_rows() == 0) {
			$htmlResult .= "<tr>";
			$htmlResult .= "<td colspan='3'>No Data</td>";
			$htmlResult .= "</tr>";
		}
		$htmlResult .= "</table>";
		echo $htmlResult;
	}

	public function index() {

		$this -> load -> view('land/land_static', $this -> data);
	}
	public function index2() {

		$this -> load -> view('land/land_static_document', $this -> data);
	}

}
